﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <sstream>

#include <QFileDialog>
#include <QStandardPaths>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QProcess>
#include <QMenu>
#include <QDebug>

#include "declarationmanager.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->initConnections ();

    ui->et_log_area->installEventFilter (this);
    ui->et_log_area->setAcceptRichText (true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initConnections()
{
    connect (ui->btn_browse, &QPushButton::clicked,
             this, &MainWindow::OnSelectLibraryPath);
    connect (ui->et_log_area, &QTextEdit::customContextMenuRequested,
             this, &MainWindow::OnLogAreaContextMenuRequest);
}

void MainWindow::analysisFiles(const QList<QUrl> &file_urls)
{
    for (auto &url: file_urls)
    {
        std::ostringstream oss;
        QString file_path = url.toLocalFile ();
        ui->statusBar->showMessage ("Analysing file: " + file_path);
        oss << "Start Analysing file: " << file_path.toStdString () << "\n\n";
        DeclarationManager::getInstance ().validationXmlScript (file_path.toStdString (), oss);
        oss << '\n' << file_path.toStdString () << " analysis finished!!\n";
        ui->et_log_area->append (QString::fromStdString (oss.str ()));
        ui->et_log_area->append (QString(128, '#'));
        ui->statusBar->clearMessage ();
    }
}

void MainWindow::OnSelectLibraryPath()
{
    auto Dir =
    QFileDialog::getExistingDirectory (this, "Select type declaration library dir",
                                       QStandardPaths::writableLocation (QStandardPaths::HomeLocation),
                                       QFileDialog::DontUseNativeDialog);
    if (Dir.isEmpty ())
        return;

    ui->et_lib_path->setText (Dir);
    DeclarationManager::getInstance ().initDeclarationLibPath (Dir.toStdString ());
}

void MainWindow::OnLogAreaContextMenuRequest(const QPoint &pos)
{
    QMenu menu(this);
    menu.addAction ("Clear", [this]{
        ui->et_log_area->clear ();
    });
    menu.exec (ui->et_log_area->mapToGlobal (pos));
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->et_log_area)
    {
        if (event->type () == QEvent::DragEnter)
        {
            auto drag_enter = dynamic_cast<QDragEnterEvent*>(event);
            if (drag_enter && drag_enter->mimeData ()->hasUrls ())
            {
                drag_enter->setDropAction (Qt::CopyAction);
                drag_enter->accept ();
                return true;
            }
        }
        else if (event->type () == QEvent::Drop)
        {
            auto drop_event = dynamic_cast<QDropEvent*>(event);
            if (drop_event)
            {
                QList<QUrl> file_urls = drop_event->mimeData ()->urls ();
                this->analysisFiles (file_urls);
            }
        }
    }
    return false;
}
